#include "PreferencesDialog.h"
#include <QRegExpValidator>
#include <QPushButton>
#include <QMessageBox>
#include <QFileDialog>


//-------------------------------------

PreferencesDialog::PreferencesDialog( ShowParam * _sp, QWidget * parent ) :
    QDialog(parent)
{
    sp = _sp;
    //resize( 800, 500 );

    glay = new QGridLayout( this );
    glay->setContentsMargins( 2, 2, 2, 2 );

    lblIP = new QLabel( "CSADA ip:", this );
    leHost = new QLineEdit( "10.0.0.39", this );
    lblPort = new QLabel( "Port:", this );
    lePort = new QLineEdit( "47001", this );
    lblDataPath = new QLabel( "Path to Data Directory:", this );
    ledAllDataPath = new QLineEdit( this );
    btnChoosePath = new QPushButton( "Choose Path", this );
    buttonBox = new QDialogButtonBox( this );
    buttonBox->setOrientation( Qt::Horizontal );
    buttonBox->setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );

    //gbTagView = new QGroupBox( "Tag View", this );
    gbAllTags = new QGroupBox( "All Tags",  this  );
    gbNodeShowTag = new QGroupBox( "Node Tag",  this  );
    gbBranchShowTag = new QGroupBox( "Branch Tag",  this  );

    glay->addWidget( lblIP,           0, 0, 1, 1 );
    glay->addWidget( leHost,          0, 1, 1, 1 );
    glay->addWidget( lblPort,         1, 0, 1, 1 );
    glay->addWidget( lePort,          1, 1, 1, 1 );
    glay->addWidget( lblDataPath,     2, 0, 1, 1 );
    glay->addWidget( ledAllDataPath,  2, 1, 1, 7 );
    glay->addWidget( btnChoosePath,   2, 8, 1, 1 );
    glay->addWidget( gbAllTags,       3, 0, 1, 9 );
    glay->addWidget( gbNodeShowTag,   4, 0, 1, 9 );
    glay->addWidget( gbBranchShowTag, 5, 0, 1, 9 );
    glay->addWidget( buttonBox,       6, 0, 1, 9 );

    QWidget * wgtDummy1;
    wgtDummy1 = new QWidget( this );
    wgtDummy1->setMinimumSize( QSize( 400, 0 ) );
    glay->addWidget( wgtDummy1, 0, 2, 2, 7 );

    chbShowNodeTags = new QCheckBox( "Show Node Tags", gbAllTags );
    chbShowBranchTags = new QCheckBox( "Show Branch Tags", gbAllTags );
    chbShowTagBorder = new QCheckBox( "Show Tag Border", gbAllTags );

    glayAllTags = new QGridLayout( gbAllTags );
    glayAllTags->setContentsMargins( 2, 2, 2, 2 );

    glayAllTags->addWidget( chbShowNodeTags,   0, 0, 1, 1 );
    glayAllTags->addWidget( chbShowBranchTags, 0, 1, 1, 1 );
    glayAllTags->addWidget( chbShowTagBorder,  0, 2, 1, 1 );

    chbUnom = new QCheckBox( "Unom", gbNodeShowTag );
    chbUmod = new QCheckBox( "Umod", gbNodeShowTag );
    chbUmin = new QCheckBox( "Umin", gbNodeShowTag );
    chbCX = new QCheckBox( "CX", gbNodeShowTag );
    chbPh = new QCheckBox( "Ph", gbNodeShowTag );
    chbQmin = new QCheckBox( "Qmin", gbNodeShowTag );
    chbUmax = new QCheckBox( "Umax", gbNodeShowTag );
    chbCX2 = new QCheckBox( "CX2", gbNodeShowTag );
    chbQh = new QCheckBox( "Qh", gbNodeShowTag );
    chbQmax = new QCheckBox( "Qmax", gbNodeShowTag );
    chbGbus = new QCheckBox( "Gbus", gbNodeShowTag );
    chbDX = new QCheckBox( "DX", gbNodeShowTag );
    chbPgen = new QCheckBox( "Pgen", gbNodeShowTag );
    chbPgmin = new QCheckBox( "Pgmin", gbNodeShowTag );
    chbBbus = new QCheckBox( "Bbus", gbNodeShowTag );
    chbIsChoosen = new QCheckBox( "isChoosen", gbNodeShowTag );
    chbQgen = new QCheckBox( "Qgen", gbNodeShowTag );
    chbPhmin = new QCheckBox( "Phmin", gbNodeShowTag );
    chbPU = new QCheckBox( "PU", gbNodeShowTag );
    chbId = new QCheckBox( "id", gbNodeShowTag );
    chbName = new QCheckBox( "name", gbNodeShowTag );

    glayNodeShowTag = new QGridLayout( gbNodeShowTag );
    glayNodeShowTag->setContentsMargins( 2, 2, 2, 2 );

    glayNodeShowTag->addWidget( chbUnom,      0, 0, 1, 1 );
    glayNodeShowTag->addWidget( chbUmod,      0, 1, 1, 1 );
    glayNodeShowTag->addWidget( chbUmin,      0, 2, 1, 1 );
    glayNodeShowTag->addWidget( chbCX,        0, 3, 1, 1 );
    glayNodeShowTag->addWidget( chbPh,        1, 0, 1, 1 );
    glayNodeShowTag->addWidget( chbQmin,      1, 1, 1, 1 );
    glayNodeShowTag->addWidget( chbUmax,      1, 2, 1, 1 );
    glayNodeShowTag->addWidget( chbCX2,       1, 3, 1, 1 );
    glayNodeShowTag->addWidget( chbQh,        2, 0, 1, 1 );
    glayNodeShowTag->addWidget( chbQmax,      2, 1, 1, 1 );
    glayNodeShowTag->addWidget( chbGbus,      2, 2, 1, 1 );
    glayNodeShowTag->addWidget( chbDX,        2, 3, 1, 1 );
    glayNodeShowTag->addWidget( chbPgen,      3, 0, 1, 1 );
    glayNodeShowTag->addWidget( chbPgmin,     3, 1, 1, 1 );
    glayNodeShowTag->addWidget( chbBbus,      3, 2, 1, 1);
    glayNodeShowTag->addWidget( chbIsChoosen, 3, 3, 1, 1 );
    glayNodeShowTag->addWidget( chbQgen,      4, 0, 1, 1);
    glayNodeShowTag->addWidget( chbPhmin,     4, 1, 1, 1 );
    glayNodeShowTag->addWidget( chbPU,        4, 2, 1, 1 );
    glayNodeShowTag->addWidget( chbId,        4, 3, 1, 1 );
    glayNodeShowTag->addWidget( chbName,      5, 0, 1, 1 );

    chbNbegin = new QCheckBox( "Nbegin", gbBranchShowTag );
    chbState = new QCheckBox( "state", gbBranchShowTag );
    chbX = new QCheckBox( "X", gbBranchShowTag );
    chbKt = new QCheckBox( "Kt", gbBranchShowTag );
    chbNend = new QCheckBox( "Nend", gbBranchShowTag );
    chbR = new QCheckBox( "R", gbBranchShowTag );
    chbYc = new QCheckBox( "Yc", gbBranchShowTag );
    chbImax = new QCheckBox( "Imax", gbBranchShowTag );


    glayBranchShowTag = new QGridLayout( gbBranchShowTag );
    glayBranchShowTag->setContentsMargins( 2, 2, 2, 2 );

    glayBranchShowTag->addWidget( chbNbegin, 0, 0, 1, 1 );
    glayBranchShowTag->addWidget( chbState,  0, 1, 1, 1 );
    glayBranchShowTag->addWidget( chbX,      0, 2, 1, 1 );
    glayBranchShowTag->addWidget( chbKt,     0, 3, 1, 1 );
    glayBranchShowTag->addWidget( chbNend,   1, 0, 1, 1 );
    glayBranchShowTag->addWidget( chbR,      1, 1, 1, 1 );
    glayBranchShowTag->addWidget( chbYc,     1, 2, 1, 1 );
    glayBranchShowTag->addWidget( chbImax,   1, 3, 1, 1 );

    connect( buttonBox, SIGNAL(accepted()), this, SLOT(accept()) );
    connect( buttonBox, SIGNAL(rejected()), this, SLOT(reject()) );
    connect( btnChoosePath, SIGNAL( clicked() ), this, SLOT( sltChoosePath() ) );

    setWindowTitle( "catGUI Preferences" );

    updateControls();
}

//-------------------------------------

int PreferencesDialog::Exec()
{
    int res = exec();
    if( res == QDialog::Accepted )
    {
        QRegExp reg( "^(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])(\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])){3}$" );
        QRegExpValidator v( reg, 0 );
        int pos = 0;
        QString tmp = leHost->text();

        if( v.validate( tmp, pos ) == QValidator::Acceptable )
        {
            sp->host = tmp;
        }
        else
        {
            QMessageBox mb;
            mb.setText( QString( "Host '%1' is not valid" ).arg( tmp ) );
            mb.exec();
            res == QDialog::Rejected;
        }

        if( res == QDialog::Accepted )
        {
            pos = 0;
            tmp = lePort->text();
            reg = QRegExp( "[0-9]+" );
            v.setRegExp( reg );
            if( v.validate( tmp, pos ) == QValidator::Acceptable )
            {
                sp->port = tmp.toInt();
            }
            else
            {
                QMessageBox mb;
                mb.setText( QString( "Port '%1' is not valid" ).arg( tmp ) );
                mb.exec();
                res == QDialog::Rejected;
            }
        }

        if( res == QDialog::Accepted )
        {
            sp->ShowTagBorder = chbShowTagBorder->isChecked();
            sp->ShowNodeTags = chbShowNodeTags->isChecked();
            sp->ShowBranchTags = chbShowBranchTags->isChecked();

            sp->DataPath = ledAllDataPath->text();

            sp->node.Bbus = chbBbus->isChecked();
            sp->node.CX = chbCX->isChecked();
            sp->node.CX2 = chbCX2->isChecked();
            sp->node.DX = chbDX->isChecked();
            sp->node.Gbus = chbGbus->isChecked();
            sp->node.id = chbId->isChecked();
            sp->node.isChoosen = chbIsChoosen->isChecked();
            sp->node.name = chbName->isChecked();
            sp->node.Pgen = chbPgen->isChecked();
            sp->node.Pgmin = chbPgmin->isChecked();
            sp->node.Ph = chbPh->isChecked();
            sp->node.Phmin = chbPhmin->isChecked();
            sp->node.PU = chbPU->isChecked();
            sp->node.Qgen = chbQgen->isChecked();
            sp->node.Qh = chbQh->isChecked();
            sp->node.Qmax = chbQmax->isChecked();
            sp->node.Qmin = chbQmin->isChecked();
            sp->node.Umax = chbUmax->isChecked();
            sp->node.Umin = chbUmin->isChecked();
            sp->node.Umod = chbUmod->isChecked();
            sp->node.Unom = chbUnom->isChecked();

            sp->branch.Bstate = chbState->isChecked();
            sp->branch.Imax = chbImax->isChecked();
            sp->branch.Kt = chbKt->isChecked();
            sp->branch.Nbegin = chbNbegin->isChecked();
            sp->branch.Nend = chbNend->isChecked();
            sp->branch.R = chbR->isChecked();
            sp->branch.X = chbX->isChecked();
            sp->branch.Yc = chbYc->isChecked();
        }
    }
    return res;
}

//-------------------------------------

void PreferencesDialog::updateControls()
{
    chbShowTagBorder->setChecked( sp->ShowTagBorder );
    chbShowNodeTags->setChecked( sp->ShowNodeTags );
    chbShowBranchTags->setChecked( sp->ShowBranchTags );

    chbBbus->setChecked( sp->node.Bbus );
    chbCX->setChecked( sp->node.CX );
    chbCX2->setChecked( sp->node.CX2 );
    chbDX->setChecked( sp->node.DX );
    chbGbus->setChecked( sp->node.Gbus );
    chbId->setChecked( sp->node.id );
    chbIsChoosen->setChecked( sp->node.isChoosen );
    chbName->setChecked( sp->node.name );
    chbPgen->setChecked( sp->node.Pgen );
    chbPgmin->setChecked( sp->node.Pgmin );
    chbPh->setChecked( sp->node.Ph );
    chbPhmin->setChecked( sp->node.Phmin );
    chbPU->setChecked( sp->node.PU );
    chbQgen->setChecked( sp->node.Qgen );
    chbQh->setChecked( sp->node.Qh );
    chbQmax->setChecked( sp->node.Qmax );
    chbQmin->setChecked( sp->node.Qmin );
    chbUmax->setChecked( sp->node.Umax );
    chbUmin->setChecked( sp->node.Umin );
    chbUmod->setChecked( sp->node.Umod );
    chbUnom->setChecked( sp->node.Unom );

    chbState->setChecked( sp->branch.Bstate );
    chbImax->setChecked( sp->branch.Imax );
    chbKt->setChecked( sp->branch.Kt );
    chbNbegin->setChecked( sp->branch.Nbegin );
    chbNend->setChecked( sp->branch.Nend );
    chbR->setChecked( sp->branch.R );
    chbX->setChecked( sp->branch.X );
    chbYc->setChecked( sp->branch.Yc );

    leHost->setText( sp->host );
    lePort->setText( QString::number( sp->port ) );
    ledAllDataPath->setText( sp->DataPath );
}

//-------------------------------------

void PreferencesDialog::sltChoosePath()
{
    QFileDialog fd;
    fd.setOption( QFileDialog::ShowDirsOnly, true );
    QString path = fd.getExistingDirectory( this, "Choose catGUI Data Directory" );
    if( !path.isEmpty() )
        ledAllDataPath->setText( path );
}

//-------------------------------------s
